package com.linkage.bookshop.service.impl;

import java.util.List;
import java.util.Date;

import javax.servlet.http.Cookie;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linkage.bookshop.model.Book;
import com.linkage.bookshop.model.Order;
import com.linkage.bookshop.model.Orderline;
import com.linkage.bookshop.model.Trolleyline;
import com.linkage.bookshop.repository.OrderRepository;
import com.linkage.bookshop.repository.UserRepository;
import com.linkage.bookshop.service.inter.BookService;
import com.linkage.bookshop.service.inter.OrderService;
import com.linkage.bookshop.service.inter.TrolleyService;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderRepository orderRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private BookService bookService;
	@Autowired
	private TrolleyService trolleyService;

	/* 订单提交 */
	@Override
	public int submitOrder(int bookId, int orderlineAmount,
			String username, String receiverInfo) {
		/* 验证是否有此用户 */
		Integer userId = userRepository.selectUserId(username);
		if (userId == null) {
			return 0;
		}

		/* 验证是否存在此书 */
		Book book = new Book();
		book = bookService.loadBookDetail(bookId);
		if (book == null) {
			return 0;
		}
		/* 验证json数据格式 */
		String[] ss = new String[4];
		ss = receiverInfo.split(",");
		if (!ss[0].contains("receiver_name:")
				|| !ss[1].contains("receiver_addr:")
				|| !ss[2].contains("receiver_phonenum:")
				|| !ss[3].contains("note:")) {
			return 0;
		}
		/* 插入数据库 */
		Order order = new Order();
		Orderline ol = new Orderline();
		double bookPrice = book.getOutPrice();
		order.setPrice(bookPrice * orderlineAmount);
		order.setStatus(1);
		order.setReceiverInfo(receiverInfo);
		order.setCreateTime(new Date());
		order.setUserId(userId);
		orderRepository.insertOrder(order);
		ol.setOrderId(order.getId());
		ol.setAmount(orderlineAmount);
		ol.setBook(book);
		ol.setBookPrice(bookPrice);
		ol.setCommentFlag(false);
		orderRepository.insertOrderline(ol);
		return order.getId();

	}
	
	/*通过购物车提交订单*/
	public int submitOrder(List<Trolleyline> tls,String receiverInfo,String username){
		int userId = userRepository.selectUserId(username);
		Order order = new Order();
		Orderline ol = new Orderline();
		double allPrice=0;
		for(int i=0;i<tls.size();i++){
			allPrice=allPrice+tls.get(i).getBookPrice();			
		}
		order.setPrice(allPrice);
		order.setStatus(1);
		order.setReceiverInfo(receiverInfo);
		order.setCreateTime(new Date());
		order.setUserId(userId);
		orderRepository.insertOrder(order);
	    for(int i=0;i<tls.size();i++){
	    	boolean b = true;
			bookService.modifyVolume(tls.get(i).getBook().getId(), tls.get(i).getAmount(), b);
	    	ol.setOrderId(order.getId());
			ol.setAmount(tls.get(i).getAmount());
			ol.setBook(tls.get(i).getBook());
			ol.setBookPrice(tls.get(i).getBookPrice());
			ol.setCommentFlag(false);
			orderRepository.insertOrderline(ol);
	    }
	    return order.getId();
		
	}

	/* 确认收货 */
	@Override
	public boolean confirmReceipt(int orderId) {
		Order order = new Order();
		/* 验证是否有此订单 */
		order = orderRepository.selectOrderById(orderId);
		if (order == null) {
			return false;
		}
		order.setStatus(5);
		orderRepository.updateOrder(order);
		return true;
	}

	/* 查看订单具体信息 */
	@Override
	public List<Orderline> checkOrderline(int orderId) {
		List<Orderline> ols=orderRepository.checkOrderline(orderId);
		return ols;

	}

	/* 列出某用户订单列表 */
	@Override
	public List<Order> listOrders(String username) {
		/* 通过用户名找到用户ID */

		int userId = userRepository.selectUserId(username);
		List<Order> orders = orderRepository.selectOrderByUserId(userId);
		return orders;

	}

	/* 按状态列出所有订单 */
	@Override
	public List<Order> listAllOrders(int orderStatus) {
		List<Order> orders = orderRepository.selectOrderByStatus(orderStatus);
		return orders;
	}

	/* 改变订单状态 */
	@Override
	public boolean changeOrderStatus(int orderStatus, int orderId) {
		Order order = new Order();
		/* 验证是否有此订单 */
		order = orderRepository.selectOrderById(orderId);
		if (order == null) {
			return false;
		}
		if (orderStatus < 0 || orderStatus > 5) {
			return false;
		}
		order.setStatus(orderStatus);
		orderRepository.updateOrder(order);
		return true;
	}

	public boolean submitTrolley(Cookie[] cookies, String username,
			String receiverInfo) {
		/* 验证是否有此用户 */
		Integer userId = userRepository.selectUserId(username);
		if (userId == null) {
			return false;
		}
		/* 验证json数据格式 */
		String[] ss = new String[4];
		ss = receiverInfo.split(",");
		if (!ss[0].contains("receiver_name:")
				|| !ss[1].contains("receiver_addr:")
				|| !ss[2].contains("receiver_phonenum:")
				|| !ss[3].contains("note:")) {
			return false;
		}
		/* 插入数据库 */
		List<Trolleyline> tls = trolleyService.checkTrolley(cookies, username);
		Order order = new Order();
		double bookPrice = 0;
		for (int i = 0; i < tls.size(); i++) {
			bookPrice = bookPrice + tls.get(i).getBookPrice();
		}
		order.setPrice(bookPrice);
		order.setStatus(1);
		order.setReceiverInfo(receiverInfo);
		order.setCreateTime(new Date());
		order.setUserId(userId);
		orderRepository.insertOrder(order);
		return true;

	}
	public boolean pay(double price,String username){
		double oldWealth=userRepository.selectWealth(username);
		if(oldWealth<price){
			return false;
		}
		double wealth=oldWealth-price;
		orderRepository.modifyWealth(username, wealth);
	    return true;
	}
	
	
	public void changeCommentFlag(int orderlineId){
		orderRepository.changeCommentFlag(orderlineId);
	}
	


}
